<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Transaction extends Model
{
    protected $table = 'transactions';
    
    protected $fillable = [
        'room_id', 'from_user_id', 'to_user_id', 'amount', 
        'type', 'description', 'create_time'
    ];
    
    public $timestamps = false;
    
    protected $primaryKey = 'id';
    
    // 交易类型常量
    const TYPE_BET = 1;     // 下注
    const TYPE_WIN = 2;     // 赢钱
    const TYPE_TRANSFER = 3; // 转账
    const TYPE_SETTLE = 4;  // 结算
    
    /**
     * 获取交易发起用户
     */
    public function fromUser()
    {
        return $this->belongsTo(User::class, 'from_user_id');
    }
    
    /**
     * 获取交易接收用户
     */
    public function toUser()
    {
        return $this->belongsTo(User::class, 'to_user_id');
    }
    
    /**
     * 获取关联的房间
     */
    public function room()
    {
        return $this->belongsTo(Room::class, 'room_id');
    }
    
    /**
     * 获取交易类型名称
     */
    public function getTypeName()
    {
        $types = [
            self::TYPE_BET => '下注',
            self::TYPE_WIN => '赢钱',
            self::TYPE_TRANSFER => '转账',
            self::TYPE_SETTLE => '结算'
        ];
        
        return $types[$this->type] ?? '未知';
    }
}